<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


class Aenquiry extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("UsersInquiry");
        parent::__construct();

    }

    /**
     * @return \think\response\Json  开仓委托
     */
    public function index()
    {//
        //开仓
      //$where['a.status'] =['in','0,1'];
    //  $where['a.status'] =['in','0,1'];

      $where['sp.status'] =[["in",'0'],['exp',"is null"],'or'];
      return $this->baseXIndex($where);

    }

    public function succeed(){
        $where['sp.status'] =['in',[1,2]];
        return $this->baseXIndex($where);
    }



    private function baseXIndex($where=[]){
        //搜索条件 时间 用户号码


        if(isset($this->mobile)){
            $where['u.mobile']=['like','%'.$this->mobile.'%'];
        }

        if(isset($this->auto)){
            $where['a.auto']=$this->auto;
        }
        $t = 60*60*24-1;
        if(isset($this->start_time)||isset($this->end_time))
        {
            if(isset($this->start_time)&&isset($this->end_time)&&($this->start_time==$this->end_time)){

                $this->end_time = $this->start_time;
                $where['a.created_at']=['between',[strtotime($this->start_time),strtotime($this->end_time)+$t]];
            }else{
                $where['a.created_at']=['between',[isset($this->start_time)?strtotime($this->start_time):strtotime("1970/1/1"),isset($this->end_time)?strtotime($this->end_time)+$t:strtotime("3030/11/1")]];
            }


        }


        if(isset($this->status)){
            if($this->status==0){
                $where['sp.status'] = array('exp','is  null');

            }
            else{
                $where['sp.status'] = $this->status;
            }
        }

        //时间的处理
        $res = $this->model->enquiry_page($where);

        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }


    /**
     * @return \think\response\Json 确认回执
     */
    public function confirmstatus()
    {

    //验证当前是否存在

        // 更新授权价格
        $UsersAskpriceModel =model("UsersAskprice");
        $res = $this->model->where(["id"=>$this->inquir_id])->find();
        if(empty($res)){
            return   failJson($this->ecLang("queryfail"));
        }
        if($res['status']){
            return failJson($this->ecLang("statuswith"));
        }
        //10001
        $data =[
            "status"=>1,
            "manage_per"=>$this->manage_per,//管理率
            "confirm_time"=>time(),
            "manage_money"=>$this->manage_money,//管理费用 权利金
            "inquir_id"=>$this->inquir_id,//
            "protocol_id"=>$this->protocol_id,//
        ];

        $count = $UsersAskpriceModel->baseCount(["inquir_id"=>$this->inquir_id]);

        if($count>0)
        {
            return   failJson("该询价已经在队列中..请等待");
        }
       $CodeResult =  $this->model->getCodeInfo(["a.id"=>$this->inquir_id]);

        $danWei = $CodeResult['mouth_config_id']>12?"天":"月";
        $ContentFormt = Config::get("senddx")['confirmstatus'];
        $content =  sprintf($ContentFormt,$CodeResult['mobile'],$CodeResult['name'],$CodeResult['code'],$CodeResult['mouth_config_id'],$danWei);
       $res_ = $UsersAskpriceModel->save($data);
       $result = !empty($res_)?"成功":"失败";
        w_s("resnd",["content"=>"{$content},{$result}","user_id"=>$res['user_id']]);
        if(empty($res_))
        {
            return failJson($this->ecLang("failupdate"));
        }

        return   sucessJson($this->ecLang("scuessupdate"));
    }


}
